﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class RoleApiController : ApiController
    {
        public List<Role> Get()
        {
            try
            {
                return new RoleRepository().GetList();
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public RoleRepository Get(int? pageNumber, string orderBy, string orderDirection, string roleName)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(roleName))
                    whereClause = String.Format(whereClause + " AND RoleName.Contains(\"{0}\")", roleName);

                return new RoleRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public Role Get(int? id)
        {
            try
            {
                if (!id.HasValue || id == 0) return new Role();

                var roleRepository = new RoleRepository();
                var role = roleRepository.GetByID(id.Value);
                if (role != null)
                {
                    var rpRepository = new RolePermissionRepository();
                    role.RolePermissions = rpRepository.GetByRoleID(role.RoleID);

                    var urRepository = new UserRoleRepository();
                    role.UserRoleComplexs = urRepository.GetByRoleID(role.RoleID);
                }

                return role;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (!id.HasValue || id == 0)
                {
                    var roleRepository = new RoleRepository();
                    roleRepository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public RoleRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string roleName)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var roleRepository = new RoleRepository();
                roleRepository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection, roleName);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public Role Put(Role role)
        {
            try
            {
                var roleRepository = new RoleRepository();
                int roleID = roleRepository.Update(role);

                return Get(roleID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }
    }
}
